﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using HossMvp.Core;

namespace HossMvp.WinForms
{
    public class WinFormsCommandProcessor : CommandProcessor
    {
        public WinFormsCommandProcessor(NameDecorator decorator)
            : base(decorator)
        {
        }

        public override ICommand RouteCommand(object ctrl, int eventType, string name)
        {
            var command = base.RouteCommand(ctrl, eventType, name);
            if (command != null)
            {
                dynamic control = ctrl;
                switch (eventType)
                {
                    case WinFormsEventTypeConstants.FormClosing:
                        control.FormClosing += this.MakeFormClosingHandler(command, eventType);
                        break;
                    case WinFormsEventTypeConstants.ItemCheck:
                        control.ItemCheck += this.MakeItemCheckHandler(command, eventType);
                        break;
                    case WinFormsEventTypeConstants.SelectedIndexChanged:
                        control.SelectedIndexChanged += this.MakeHandler(command, eventType);
                        break;
                    default:
                        return command;
                }
            }

            return null;    // routed
        }

        private dynamic MakeItemCheckHandler(ICommand command, int eventType)
        {
            ItemCheckEventHandler result = (s, e) =>
            {
                if (command != null)
                {
                    var context = new WinFormsCommandContext { Sender = s, Arguments = e };
                    command.Execute(context);
                }
                else
                {
                    Debug.Assert(false, "No such command!");
                }
            };

            return result;
        }

        private dynamic MakeFormClosingHandler(ICommand command, int eventType)
        {
            FormClosingEventHandler result = (s, e) =>
            {
                if (command != null)
                {
                    var context = new WinFormsCommandContext { Sender = s, Arguments = e };
                    command.Execute(context);
                }
                else
                {
                    Debug.Assert(false, "No such command!");
                }
            };

            return result;
        }

        protected override dynamic MakeHandler(ICommand command, int eventType)
        {
            EventHandler result = (s, e) =>
            {
                if (command != null)
                {
                    var context = new WinFormsCommandContext { Sender = s, Arguments = e };
                    command.Execute(context);
                }
                else
                {
                    Debug.Assert(false, "No such command!");
                }
            };
            return result;
        }
    }
}
